package site.icesky.springboot.mybatis.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import site.icesky.springboot.mybatis.dao.pojo.Users;
import site.icesky.springboot.mybatis.service.UsersService;

import java.util.List;

/**
 * @ClassName: UsersController <br>
 * @Description: 请求 <br>
 * @Package: com.isstudy.s12mybastis.controller <br>
 * @author: suitianmou <br>
 * @Date: 2020/5/13-00:23 <br>
 * @version: 1.0 <br>
 * @see <br>
 * @since (从产品的哪一个版本此类被添加进来 ( 可选))<br>
 * 修改历史: <br>
 * 修改时间             修改人           版本变更           修改说明<br>
 * -----------------------------------------------------<br>
 * <br>
 */
@Controller
@RequestMapping("/users")
public class UsersController {
    @Autowired
    private UsersService usersService;

    /**
     * 页面跳转
     * @param page
     * @return
     */
    @RequestMapping("/{page}")
    public String showPage(@PathVariable String page){
        return page;
    }

    /**
     * 增加用户
     * @param users
     * @return
     */
    @RequestMapping("/addUser")
    public String addUser(Users users){
        this.usersService.addUser(users);
        return "ok";
    }

    /**
     * 查询所有用户
     * @param model
     * @return
     */
    @RequestMapping("/findUserAll")
    public String findUserAll(Model model){
        List<Users> list =  this.usersService.findAllUsers();
        model.addAttribute("list", list);
        return "showUsers";
    }

    @RequestMapping("/findUserById")
    public String findUserById( Integer id, Model model){
        Users user = this.usersService.findUserById( id );
        model.addAttribute("user", user);
        return "edit";
    }

    @RequestMapping("editUser")
    public String  updateUserById(Users user){
        this.usersService.updateUser(user);
        return "ok";
    }
    @RequestMapping("deleteUser")
    public String deleteUserById( Integer id){
        this.usersService.deleteUserById(id);
        return "ok";
    }
}
